package common;

import java.io.*;
import java.util.*;


import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import javax.mail.Message;
import javax.mail.MessagingException;




public class SendMail {
	
	public static boolean sendMailToUser(String title, String content, String member_nm, String email_id,String balsongId)
	        throws MessagingException {
	        /*
	         * Session을 생성하기 위해 java.util.Properties 클래스를 생성하고 자신이 해당하는 SMTP 호스트 주소를 할당
	         */
		StringBuffer sb = new StringBuffer();
		Properties props = new Properties();
		props.put("mail.smtp.host", "localhost");

		Session msgSession = Session.getDefaultInstance(props, null);

	        try {
	            // Message 클래스의 객체를 Session을 이용해 생성
	            MimeMessage msg = new MimeMessage(msgSession);

				InternetAddress from = new InternetAddress(balsongId, "EDUBOX");
				msg.setFrom(from);

		        InternetAddress to = new InternetAddress(email_id);
		        msg.setRecipient(Message.RecipientType.TO, to);

		        msg.setSubject(title);

		       
				 sb.append("<jsp:include page='/jsp/edu_mail_form.jsp'><jsp:param name='member_nm' value='<%=member_nm%>' /><jsp:param name='content' value='<%=content%>' /></jsp:include> \n");
				
				System.out.println(sb.toString());

	            msg.setContent(sb.toString(), "text/html; charset=utf-8");

	            Transport.send(msg);

	        } catch (MessagingException e) {
	            System.out.print("SurveyResult sendMailToUser() MessagingException ====>");
	            System.out.print(e);
	            System.out.print("  e_mail ==> ");
	           
	            return false;
	        } catch (UnsupportedEncodingException e) {
	            System.out.print("SurveyResult sendMailToUser() UnsupportedEncodingException ====>");
	            System.out.print(e);
	            System.out.print("  e_mail ==> ");
	           

	            return false;
	        }

	        return true;
	    }		
}
